//
//  HLWolcomeViewController.swift
//  HLwWeibo
//
//  Created by hqhxu on 16/2/29.
//  Copyright © 2016年 hqhxu. All rights reserved.
//

import UIKit

import SDWebImage

import SnapKit

class HLWolcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpLayout()
        
        HLAccountViewModel.shareInstance.loadUserInform { (error) -> () in
            if error != nil {
                print("HLWolcomeViewController控制器收到错误：\(error)")
                return
            }
            
            let urlStr = HLAccountViewModel.shareInstance.userAcount?.avatar_large
            
            let url = NSURL(string: urlStr!)
            
            self.iconView.sd_setImageWithURL(url!)
        }
        
        
    }
    
    /// 控制器的view已经完全显示的时候调用
    override func viewDidAppear(animated: Bool) {
        
        iconViewAnimation()
        
    }
    
    /// 头像动画
    func iconViewAnimation() {
        
        UIView.animateWithDuration(1.25, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //不能直接再创建一个约束 只能是改变之前的约束
            let bottomContant = -(UIScreen.mainScreen().bounds.height - 160)
            
            self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                
                make.bottom.equalTo(self.view.snp_bottom).offset(bottomContant)
            })
            
            // 如果需要更新约束的时候更新
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                //文字的动画
                UIView.animateKeyframesWithDuration(0.25, delay: 0.1, options: UIViewKeyframeAnimationOptions(rawValue: 0), animations: { () -> Void in
                    self.messageLable.alpha = 1
                    }, completion: { (_) -> Void in
                        //销毁当前控制器 显示main控制器
                        UIApplication.sharedApplication().keyWindow?.rootViewController = HLMainController()
                })
        }
    }
    
    
    
    
    func setUpLayout() {
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(messageLable)
        
        
        backImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            
            make.bottom.equalTo(view.snp_bottom).offset(-160)
        
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        

        messageLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        messageLable.sizeToFit()
    
    }
    
    
    
    var iconContrainButton:NSLayoutConstraint?
    
    lazy var backImageView:UIImageView = UIImageView(image:UIImage(named: "ad_background"))
    
    lazy var iconView:UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named:"avatar_default_big"))
        
        imageView.layer.cornerRadius = 42.5
//        imageView.clipsToBounds = true
        imageView.layer.masksToBounds = true
        
        return imageView
    }()

    
   
    
    lazy var messageLable: UILabel = {
        
        let lable = UILabel()
        lable.text = "欢迎归来"
        lable.textColor = UIColor.blackColor()
        lable.alpha = 0
        lable.font = UIFont.systemFontOfSize(14)
        
        return lable
    }()
    

}
